# frozen_string_literal: true

class CouponsController < Admin::ApplicationController
  def index
    @page_is = 'index'
    # @coupons = Coupon.includes(:user).all.order(created_at: :desc).page(params[:page]).per(100)
    result = Coupon.group(:name, :price, :begin_date, :end_date, :region, :kind, :state)
                   .select('name,price,begin_date,end_date,region,kind,count(name) as number,state ')
    @coupons = result.map do |_coupon|
      i = Coupon.where(name: _coupon.name, receive: true).count
      _coupon.receive_count = i
      _coupon
    end
  end

  def new
    @pictures = Picture.all
  end

  def list_create
    number = params.delete(:number)
    ApplicationJob.perform_later(number, coupon_params.to_h)
  end

  def edit
    params
    @pictures = Picture.all
  end

  def update
    @coupons = if params[:region].blank?
                 Coupon.where(
                   name: params[:name],
                   price: params[:price],
                   begin_date: params[:begin_date],
                   end_date: params[:end_date],
                   kind: params[:kind]
                 )
               else
                 Coupon.where(
                   name: params[:name],
                   price: params[:price],
                   begin_date: params[:begin_date],
                   end_date: params[:end_date],
                   region: params[:region],
                   kind: params[:kind]
                 )
                end
    @coupons.update_all(url: params[:url])
    redirect_to list_coupons_url
  end

  def total
    @coupons = Coupon.where(
      name: params[:name],
      price: params[:price],
      begin_date: params[:begin_date],
      end_date: params[:end_date],
      region: params[:region],
      kind: params[:kind]
    ).order(created_at: :desc).page(params[:page]).per(100)
  end

  def list
    @page_is = 'list'
    result = Coupon.group(:name, :price, :begin_date, :end_date, :region, :kind, :state)
                   .select('name,price,begin_date,end_date,region,kind,count(name) as number,state ')
    @coupons = result.map do |_coupon|
      i = Coupon.where(name: _coupon.name, receive: true).count
      _coupon.receive_count = i
      _coupon
    end
  end

  def hide
    @coupons = if params[:state] == 'false'
                 Coupon.where(
                   name: params[:name],
                   price: params[:price],
                   begin_date: params[:begin_date],
                   end_date: params[:end_date],
                   region: params[:region],
                   kind: params[:kind]
                 ).update_all(state: true)
               else
                 Coupon.where(
                   name: params[:name],
                   price: params[:price],
                   begin_date: params[:begin_date],
                   end_date: params[:end_date],
                   region: params[:region],
                   kind: params[:kind]
                 ).update_all(state: false)
              end
    redirect_to list_coupons_url
  end

  def destroy
    @coupons = Coupon.where(
      name: params[:name],
      price: params[:price],
      begin_date: params[:begin_date],
      end_date: params[:end_date],
      region: params[:region],
      kind: params[:kind]
    ).delete_all
    redirect_to list_coupons_url
  end

  def show
    @coupons = Coupon.where(
      name: params[:name],
      price: params[:price],
      begin_date: params[:begin_date],
      end_date: params[:end_date],
      region: params[:region],
      kind: params[:kind],
      receive: true
    )
  end

  def export
    @coupons = Coupon.where(
      name: params[:name],
      price: params[:price],
      begin_date: params[:begin_date],
      end_date: params[:end_date],
      region: params[:region],
      kind: params[:kind],
      receive: true
    )
    @xls_file_name = Time.new.strftime('%Y-%m-%d %H:%M:%S') + '优惠劵信息表'
    respond_to do |format|
      format.xls  { response.headers['Content-Disposition'] = 'attachment; filename="' + @xls_file_name + '.xls"' }
    end
  end

  def coupons_export
    @coupons = Coupon.where(
      name: params[:name],
      price: params[:price],
      begin_date: params[:begin_date],
      end_date: params[:end_date],
      region: params[:region],
      kind: params[:kind]
    )
    @xls_file_name = Time.new.strftime('%Y-%m-%d %H:%M:%S') + '优惠劵信息表'
    respond_to do |format|
      format.xls  { response.headers['Content-Disposition'] = 'attachment; filename="' + @xls_file_name + '.xls"' }
    end
  end

  def import
    uploaded_io = params[:file]
    type = uploaded_io.content_type
    convert_to_nice_date = Time.now.strftime('%Y/%m/%d')
    store_dir = "system/uploads/#{convert_to_nice_date}"
    FileUtils.mkdir_p("#{Rails.root}/public/#{store_dir}")
    file_name = uploaded_io.original_filename
    File.open(Rails.root.join('public', store_dir.to_s, uploaded_io.original_filename), 'wb') do |file|
      file.write(uploaded_io.read)
    end
    path = "#{Rails.root}/public/#{store_dir}/#{file_name}"
    s = SimpleSpreadsheet::Workbook.read(path)
    s.first_row.upto(s.last_row) do |line|
      next unless line > 1
      i = line.to_i - 1
      Coupon.find_or_create_by(
        show_index: i,
        name: s.cell(line, 1, 1),
        code: s.cell(line, 2, 1),
        purpose: s.cell(line, 3, 1),
        begin_date: s.cell(line, 4, 1),
        end_date: s.cell(line, 5, 1),
        kind: s.cell(line, 6, 1),
        region: s.cell(line, 7, 1),
        limit_state: s.cell(line, 8, 1),
        price: s.cell(line, 9, 1)
      )
    end
    render text: 'success!'
  end

  def coupon_params
    params.permit(:name, :limit_state, :purpose, :begin_date, :end_date, :region, :kind, :price, :url)
  end
 end
